var app = getApp();
var $ = require("../../utils/httpHelper.js")
var wxParse = require("../../wxParse/wxParse.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    classinfo:'',
    height: 0,
    dou:0,
    ispwddialog:false,
    isdou:true,
    pwd:'',
    paytype:0,
    class_id:''
  },
  radioChange: function (e) {
    console.log('radio发生change事件，携带value值为：', e.detail.value)
    this.setData({
      paytype: e.detail.value
    })
    if (e.detail.value == 0){
      this.setData({
        isdou:true,
      })
    }else{
      this.setData({
        isdou: false,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;
    wx.getSystemInfo({
      success: function (res) {
        console.log(res);
        self.setData({ height: res.windowHeight })
      }
    })
    if(options.id){
      let params = {
        rd_session: app.globalData.userInfo.rd_session,
        id: options.id
      }
      $.httpPost("class_detail", params, (data, err) => {
        this.setData({
          classinfo: data.data.class_info,
          class_id: options.id
        })
      })
      let par = {
        rd_session: app.globalData.userInfo.rd_session,
      }
      $.httpPost("member_info", par, (data, err) => {
        this.setData({
          dou: data.data.balance
        })
      })
    }

  },
  getpwd(e){
    this.setData({
      pwd:e.detail.value
    })
  },
  diaok(){
    this.setData({
      ispwddialog: false
    })
    this.pay()
  },
  cancel(){
    this.setData({
      ispwddialog: false
    })
  },
  submit(){
    if(this.data.isdou){
      let par = {
        rd_session: app.globalData.userInfo.rd_session
      }
      $.httpPost("check_pass", par, (data, err) => {
        if(data.data){
          this.setData({
            ispwddialog: true
          })
        }else{
          wx.showModal({
            title: '温馨提示',
            content: '请先设置密码',
            success: function (res) {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/pages/mine/passedit',
                })
              } else if (res.cancel) {
                
              }
            }
          })
        }
      })
      
    }else{
      this.pay()
    }
  },
  pay(){
    let params = {
      rd_session: app.globalData.userInfo.rd_session,
      class_id: this.data.class_id,
      pay_pass:this.data.pwd,
      pay_type:this.data.paytype
    }
    $.httpPost("submit_class_order", params, (data, err) => {
      if (data.result == 'success') {
        if(this.data.paytype == 1){
          app.wxPayClass({
            order_no: data.data.order_no, openid: app.globalData.userInfo.openId,
            success: function () {
              wx.showToast({
                title: '支付成功哈',
              })
              wx.switchTab({
                url: '/pages/mine/mine',
              })
            },
            fail: function () {
              wx.showToast({
                title: '支付失败了呢',
              })
            }
          })
        }else{
          wx.showToast({
            title: '支付成功哈',
          })
          wx.switchTab({
            url: '/pages/mine/mine',
          })
        }
      }else{
        wx.showToast({
          title: data.message,
        })
      }
    })
  }
  
})